﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.Common;
using Ice.Toolkit.Data.Entity;

namespace Ice.Toolkit.Data.Sql
{
    internal class BuilderBase<T> where T : EntityBase<T>, new()
    {
        public String SQLText { get; protected set; }

        private List<DbParameter> _dbParameters;

        public DbParameter[] DbParameters
        {
            get { return _dbParameters.ToArray(); }
            protected set { }
        }

        private DataContext _dataContext;

        protected DataContext DataContext
        {
            get { return _dataContext; }
            private set { }
        }

        public BuilderBase(DataContext context)
        {
            _dataContext = context;
            _dbParameters = new List<DbParameter>();
        }
        public virtual String Build()
        {
            return String.Empty;
        }

        protected void AddParameter(string name, object val)
        {
            _dbParameters.Add(DataContext.CreateParameter("@" + name, val));
        }

        protected string RemoveTail(string source, string tail)
        {
            source = source.Trim();
            tail = tail.Trim();

            var tmp = source.Substring(source.Length - tail.Length);

            if (tail == tmp)
            {
                return source.Substring(0, source.Length - tail.Length).Trim();
            }
            return source;
        }

        public override string ToString()
        {
            return SQLText;
        }
    }
}
